window.Control = function()
{
    this.M = null;
};

Control.prototype = {

    /** 设置当前数据*/
    setItem : function( item )
    {
        this.M.setData( item );
    },

    /** 获取数据模型初始数据*/
    getItem : function()
    {
        return this.M.getData();
    },

    /** 设置当前数据*/
    setCondItem : function( item )
    {
        this.M.setCondItem( item );
    },

    /** 获取数据模型初始数据*/
    getCondItem : function()
    {
        return this.M.getCondItem();
    },

    /** 设置当前数据*/
    setTmpCondItem : function( item )
    {
        this.M.setTmpCondItem( item );
    },

    /** 获取数据模型初始数据*/
    getTmpCondItem : function()
    {
        return this.M.getTmpCondItem();
    },

    setSubmitConditionItem : function( items )
    {
        this.M.setSubmitCondition( items );
    },

    getSubmitConditionItem : function()
    {
        return this.M.getSubmitCondition();
    },

    /** 获取数据*/
    get : function( url, data,callbackObject, type )
    {
        this.H.Request( "get", url, data, callbackObject, type || "json" );
    },
    /** 保存数据*/
    post : function( url, data,callbackObject, type )
    {
        return this.H.Request( "post", url, data, callbackObject, type || null );
    },

    /** 输出数据到模板*/
    printDataToMapPanel : function( data, template )
    {
        return this.H.getTemplate( template, data );
    },

    // 保存横向纵向纬度
    saveAxis : function( xAxis, yAxis )
    {
        var xAxisData = {};

        if( xAxis && xAxis.length )
        {
            for( var i = 0, xf; xf = xAxis[ i ]; i += 1 )
            {
                xAxisData[ xf.id ] = [ xf.getAttribute("data-step") || 1, xf.getAttribute("data-uom") || null ];
            }
        }

        var yAxisData = {};

        if( yAxis && yAxis.length )
        {
            for( var n = 0, nf; nf = yAxis[ n ]; n += 1 )
            {
                yAxisData[ nf.id ] = [ nf.getAttribute("data-step") || 1, nf.getAttribute("data-uom") || null ];
            }
        }

        var condItem = this.getCondItem();

        condItem["xAxis"] = xAxisData || {};

        condItem["yAxis"] = yAxisData || {};

        this.setCondItem( condItem );
    },

    deleteAxis : function( id )
    {
        if( id )
        {
            var condItem = this.getCondItem();

            if( condItem["xAxis"] && condItem["xAxis"][ id ] )
            {
                condItem["xAxis"][ id ] = null;

                delete condItem["xAxis"][ id ];
            }


            if( condItem["yAxis"] && condItem["yAxis"][ id ] )
            {
                condItem["yAxis"][ id ] = null;

                delete condItem["yAxis"][ id ];
            }

            this.setCondItem( condItem );
        }
    },

    resetAxis : function()
    {
        var condItem = this.getCondItem();

        condItem["xAxis"] = {};

        delete condItem["xAxis"];

        condItem["yAxis"] = {};

        delete condItem["yAxis"];

        this.setCondItem( condItem );
    },

    // 保存排序
    saveRank : function( name, value, rank )
    {
        if( name && value && rank  )
        {
            var condItem = this.getCondItem();

            condItem["ranking"] = condItem["ranking"] || {};

            condItem["ranking"][ "topColumn" ] =[ name, value, rank ] || [];

            this.setCondItem( condItem );
        }
    },

    saveConditionGroup : function( data )
    {
        if( data && data[ 0 ] )
        {
            var condItem = this.getCondItem();

            condItem["conditionGroup"] = condItem["conditionGroup"] || {};

            condItem["conditionGroup"][ data[ 0 ]["index"] ] = JSON.parse( data[ 0 ]["data"] );

            condItem["conditionGroup"][ data[ 0 ]["index"] ][ "title" ] = data[ 0 ]["title"];

            this.setCondItem( condItem );
        }
    },

    deleteConditionGroup : function( id )
    {
        if( id )
        {
            var condItem = this.getCondItem();

            if( condItem["conditionGroup"] && condItem["conditionGroup"][ id ] )
            {
                condItem["conditionGroup"][ id ] = null;

                delete condItem["conditionGroup"][ id ];
            }

            this.setCondItem( condItem );
        }
    },

    // 删除排序
    deleteRank : function()
    {
        var condItem = this.getCondItem();

        condItem["ranking"]["topColumn"] = [];

        this.setCondItem( condItem );
    },

    // 保存排序数
    saveTopNum : function( value )
    {
        if( value )
        {
            var condItem = this.getCondItem();

            condItem["ranking"] = condItem["ranking"] || {};

            condItem["ranking"][ "topNum" ] = value;

            this.setCondItem( condItem );
        }
    },

    // 保存统计
    saveCount : function( attr, value )
    {
        if( attr )
        {
            var condItem = this.getCondItem();

            condItem["count"] = condItem["count"] || {};

            condItem["count"][ attr ] = value || "NULL";

            this.setCondItem( condItem );
        }
    },

    // 保存统计值方式
    saveValueAxis : function( name, values, uom )
    {
        if( name )
        {
            var condItem = this.getCondItem();

            condItem["vAxis"] = condItem["vAxis"] || {};

            condItem["vAxis"][ name ] = [ values || "NULL", uom || "NULL"];

            this.setCondItem( condItem );

        }
    },

    deleteValueAxis : function( id )
    {
        if( id )
        {
            var condItem = this.getCondItem();

            if( condItem["vAxis"] && condItem["vAxis"][ id ] )
            {
                condItem["vAxis"][ id ] = null;

                delete condItem["vAxis"][ id ];
            }

            this.setCondItem( condItem );
        }
    },

    resetValueAxis : function()
    {
        var condItem = this.getCondItem();

        condItem["vAxis"] = {};

        delete condItem["vAxis"];

        this.setCondItem( condItem );
    },

    // 保存统计值方式
    saveTmpValueAxis : function( name, arr )
    {
        if( name )
        {
            var condItem = this.getTmpCondItem();

            condItem["vAxis"] = condItem["vAxis"] || {};

            condItem["vAxis"][ name ] = arr || [];

            this.setTmpCondItem( condItem );

        }
    },

    deleteTmpValueAxis : function( id )
    {
        if( id )
        {
            var condItem = this.getTmpCondItem();

            if( condItem["vAxis"] && condItem["vAxis"][ id ] )
            {
                condItem["vAxis"][ id ] = null;

                delete condItem["vAxis"][ id ];
            }

            this.setTmpCondItem( condItem );
        }
    },

    resetTmpValueAxis : function()
    {
        var condItem = this.getTmpCondItem();

        condItem["vAxis"] = {};

        delete condItem["vAxis"];

        this.setTmpCondItem( condItem );
    },

    // 保存筛选条件
    saveWhere : function( data )
    {
        if( data )
        {
            var condItem = this.getCondItem();

            condItem["where"] = data || {};
        }
    },

    // 保存UI元素
    saveUI : function( item )
    {
        var condItem = this.getCondItem();

        condItem["ui"] = item || {};

        this.setCondItem( condItem );
    },

    // 保存筛选条件
    saveDwhere : function( item )
    {
        var condItem = this.getCondItem();

        condItem["dwhere"] = item || [];

        this.setCondItem( condItem );
    },

    select2ChangeHandle : function( e, formGroup, View )
    {
        var data = {};

        for( var i = 0, f; f = formGroup[ i ]; i += 1 )
        {
            var selects = f.getElementsByTagName("select")[ 0 ];

            if( selects )
            {
                var sName = f.getAttribute("data-name");

                var result = View.$( f.getElementsByTagName("select") ).select2("val");

                if( result.length ) data[ sName ] = result;
            }
        }

        return data;
    },
    exchange :function( $a, $b ){

        if( $a.length != 1|| $b.length != 1 ) return false;
        var n = document.createElement("a");
        var p = document.createElement("a");

        $( n ).insertBefore( $a );
        $( p ).insertBefore( $b );
        $b.insertBefore( n );
        $a.insertAfter( p );

        $( n ).remove();
        $( p ).remove();

        return true;

    }

};
